<template>
  <!--
    作者：luoyiming
    时间：2020-06-25
    描述：插件中心-直播-微信直播
  -->
  <div>
    <div class="common-form d-s-c">
      <span>直播同步</span>
      <div class="ml20 flex-1 d-b-c">
        <!-- <el-button type="primary" size="small" icon="Plus" @click="synLive">同步直播</el-button> -->
        <el-button type="primary" size="small" icon="Plus" @click="createClick">创建直播</el-button>
        <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-input v-model="searchForm.search" placeholder="请输入直播间名称/主播昵称"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!--内容-->
    <div class="live-list">
      <div class="table-wrap">
        <el-table size="small" :data="tableData" border style="width: 100%" v-loading="loading">
          <el-table-column prop="roomid" label="直播间ID"></el-table-column>
          <el-table-column prop="name" label="直播间名称"></el-table-column>
          <el-table-column prop="anchorName" label="主播昵称"></el-table-column>
          <el-table-column prop="startTime" label="直播开始时间"></el-table-column>
          <el-table-column prop="endTime" label="直播结束时间"></el-table-column>
          <el-table-column prop="liveStatusText" label="直播间状态"></el-table-column>
          <el-table-column prop="auditStatusText" label="审核状态"></el-table-column>
          <el-table-column fixed="right" label="操作" width="120">
            <template #default="scope">
              <div class="table-buttons">
                <div>
                  <el-button @click="delClick(scope.row)" type="text" size="small">删除</el-button>
                </div>
                <div>
                  <el-button @click="editClick(scope.row)" type="text" size="small">编辑</el-button>
                </div>
                <div>
                  <el-button @click="openClick(scope.row)" type="text" size="small">商品</el-button>
                </div>
                <div>
                  <el-button @click="open(scope.row)" type="text" size="small" >获取推流链接</el-button>
                </div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!--分页-->
    <div class="pagination">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="curPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totalDataNumber"
      ></el-pagination>
    </div>
    <!--商品选择-->
    <ProductIndex :roomId="roomId" :isProduct="isProduct" @closeDialog="isProduct = false"></ProductIndex>
  </div>
</template>

<script>
import wxLiveApi from '@/api/wxlive.js';
import ProductIndex from "../part/Product.vue";

export default {
  components: {
    ProductIndex
  },
  data() {
    return {
      /*搜索表单对象*/
      searchForm: {},
      /*是否正在加载*/
      loading: true,
      /*一页多少条*/
      pageSize: 10,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      /*是否打开编辑弹窗*/
      openCreate: false,
      isProduct: false,
      roomId: 0,
      curModel: {},
      scene: '',
      /*已有的id*/
      exclude_ids: [],
      roomId: 0,
      url: '',
      /*列表数据*/
      tableData: [],
    };
  },
  created() {
    this.getData();
  },

  methods: {
    openClick(row){
      let self = this;
      self.roomId = row.roomid;
      self.isProduct = true;
    },
    /*查询*/
    onSubmit() {
      this.curPage = 1;
      this.getData();
    },

    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },

    /*获取列表*/
    getData() {
      let self = this;
      let Params = self.searchForm;
      Params.pageIndex = self.curPage;
      Params.pageSize = self.pageSize;
      self.loading = true;
      wxLiveApi
        .getList(Params, true)
        .then(res => {
          self.tableData = res.data.records;
          self.totalDataNumber = res.data.total;
          self.loading = false;
        })
        .catch(error => {
          self.loading = false;
        });
    },

    /*直播同步*/
    synLive() {
      let self = this;
      self.loading = true;
      wxLiveApi
        .syn({}, true)
        .then(res => {
          self.loading = false;
          self.curPage = 1;
          self.getData();
        })
        .catch(error => {
          self.loading = false;
        });
    },

    /* pushProduct(row,scene) {
      this.curModel = row;
      this.isproduct = true;
    }, */

    /*关闭编辑*/
    closeCreate() {
      this.openCreate = false;
    },

    /*打开编辑*/
    createClick() {
      this.$router.push({
        path: '/wxlive/room/create'
      });
    },

    /*打开编辑*/
    editClick(row) {
      this.$router.push({
        path: '/wxlive/room/edit',
        query: {
          liveId: row.liveId
        }
      });
    },

    /*关闭商品选择*/
    /* closeProductFunc(e) {
      this.isproduct = false;
    }, */

    open(row) {
      let self = this;
      self.loading = true;
      wxLiveApi
        .url({roomId: row.roomid}, true)
        .then(res => {
          self.loading = false;
          self.url = res.data;
          const h = this.$createElement;
          this.$msgbox({
            title: '推流地址',
            message: h('p', null, [h('span', null, '推流地址是 '), h('i', {style: 'color: teal'}, self.url)]),
            confirmButtonText: '确定',
          }).catch(() => {
          });
        }).catch(error => {
        self.loading = false;
      });
    },


    /*删除*/
    delClick: function (row) {
      let self = this;
      ElMessageBox.confirm('删除后不可恢复，确认删除该记录吗?', '提示', {
          type: 'warning'
        })
        .then(() => {
          wxLiveApi
            .deleteById({
              liveId: row.liveId
            })
            .then(data => {
              ElMessage({
                message: '删除成功',
                type: 'success'
              });
              self.getData();
            });
        });
    }
  }
};
</script>

<style scoped="scoped"></style>
